
  n=10000;
  tol=0.00001;
  cutValue=n;
  
  % r 30 numeros random de 1 a 36 ???ra calcular la prob
  r=zeros(1,30);
  %cot cambio de cotizacion diario
  cot=zeros(1,30);
  %val val(i)=val(i-1)*(1-cot(i)) --> valor del dia es el valor del dia anterior con el cambio en la cotizacion de hoy
  val=zeros(1,30);
  %ben ben(i)=val(i)-val(0)
  ben=zeros(1,30);
  %men 5000 beneficios mensuales distintos
  men=zeros(1,n);
 
  
  %a_0 cant inicial
  a_0=0;
  
  promedio=zeros(1,n);
  varianza=zeros(1,n);
  vars=zeros(1,n);
  
  meanVal = 0.0;

  for j=1:n
    last = a_0;
    r=rand(1,30)*36 + 1;
   
    %se calcula el beneficio para 30 d??as
    for i=1:30
        if ( r(i)<4 )
            cot(i)=-1/8;
        end
        if ( r(i)>=4 && r(i)<11 )
            cot(i)=0;
        end
        if ( r(i)>=11 && r(i)<27 )
            cot(i)=1/8;
        end
        if ( r(i)>=27 && r(i)<34 )
            cot(i)=1/2;
        end
        if ( r(i)>=34 )
            cot(i)=1;
        end

        val(i)=last+cot(i);
        ben(i)= val(i); % - a_0;

        last=val(i);
    end

    men(j)=ben(30); %esta es la realizaci??n
	
    %Formulas para calcular media y varianza
    %M(1) = x(1), M(k) = M(k-1) + (x(k) - M(k-1)) / k
    %S(1) = 0, S(k) = S(k-1) + (x(k) - M(k-1)) * (x(k) - M(k))
    %sigma = sqrt(S(n) / (n - 1)) % = standard deviation ...
    if(j<=1)
        promedio(1)=men(j);
        varianza(1)=0;
        vars(i)=0;
    else
        promedio(j) = promedio(j-1) + (men(j)-promedio(j-1))/j;		
        varianza(j) = varianza(j-1) + (men(j) - promedio(j-1))*( men(j) - promedio(j));
        %vars(j)=sqrt( varianza(j) / (j-1) ); % = std. deviation ...
        vars(j) = varianza(j) / (j-1);  
    end
   
    meanVal = promedio(j); 

    %break condition
    if(j>1)
        if( abs(vars(j)-vars(j-1))<tol )
            cutValue = j;
            break;
        end
    end
end
  
disp('result:');
fprintf('number of iteration steps (cut value): %d\n\n', cutValue);
fprintf('last mean value: %f\n\n', meanVal);


% x1 = men(1:cutValue);
% xx = linspace(0,1, 11)
% v = var(xx)
% vv = var(x1, 1)  

 
% mean of the monthly benefit ...
figure(9);
clf;

cDarkGrey = [0.3, 0.3, 0.3];
iStdSize = 12;

plot(1:cutValue, promedio(1:cutValue), '-', 'Color', cDarkGrey);

xlbl = xlabel('\it Numero de realizaciones n');
ylbl = ylabel('\it Beneficio medio en un mes');
set(xlbl, 'Interpreter', 'tex', 'FontName', 'Times', 'FontSize', iStdSize);
set(ylbl, 'Interpreter', 'tex', 'FontName', 'Times', 'FontSize', iStdSize);

axis normal;

% result:
% number of iteration steps (cut value): 1161
% 
% last mean value: 6.772825


% variance of the monthly benefit ...
% (result is maybe not correct!! --> use script_parte2.m)
% figure(10);
% clf;
% 
% plot(1:cutValue,vars(1:cutValue),'-','Color',cDarkGrey);
% xlbl = xlabel('\it Numero de realizaciones n');
% ylbl = ylabel('\it Varianza del beneficio en un mes');
% set(xlbl, 'Interpreter', 'tex', 'FontName', 'Times', 'FontSize', iStdSize);
% set(ylbl, 'Interpreter', 'tex', 'FontName', 'Times', 'FontSize', iStdSize);

  
